/**
 * 树zTree
 * zTreeNodeOnClick(event, treeId, treeNode) 树节点点击回调函数，自己实现
 */
var treeRootNode=[
        {code:100000, id:1, name:"中国", isParent:true, type:'0'}
    ];
var nodeList = [];
var currentTreeNode = [];//打开指定几点时的路径信息，必须保证在树的同一层中所有节点该属性是唯一的
var currentTargetAttr = "";//指定属性
var settingNew = {
			view : {
	        	showTitle: false,
		        dblClickExpand: false,
		        showLine: false,
		        showTitle: true,
		        fontCss: setFontCss
		     },
			async : {
				enable : true,
				url : getUrlNew
			},
			callback : {
				onClick : zTreeNodeOnClick,
				onAsyncSuccess: zTreeOnAsyncSuccess
			},
			conf : {
				hasEduOrgan : false,
			    hasSchool : false,
			    treeType: 1,//1表示区域树，2表示学校树
			    isChange: 0,
			    hasDepartment: false
			}
		};

/**
 * 异步加载节点url
 * @param treeId
 * @param treeNode
 * @returns {String}
 */
 function getUrlNew(treeId,treeNode){
	 if(1 == settingNew.conf.treeType){
		 return "manage/gisarea_gisAreaSchoolData.action?parentId="+treeNode.id+"&hasEduOrgan="+settingNew.conf.hasEduOrgan+"&hasSchool="+settingNew.conf.hasSchool+"&hasDepartment="+settingNew.conf.hasDepartment+"&rdm="+Math.random();
	 }
	 if(2 == settingNew.conf.treeType){
		 if(0 == treeNode.level){
			 return "manage/student_schoolTreeData.action?currentId="+treeNode.id+"&level="+treeNode.level+"&rdm="+Math.random();
		 }
		 else{
			 return "manage/student_schoolTreeData.action?currentId="+treeNode.id+"&parentId="+treeNode.parentId+"&level="+treeNode.level+"&rdm="+Math.random();
		 }
	 }
     // 获取机构和学校的树
	 if(3 == settingNew.conf.treeType){
		 return "manage/eduorg_gisEduOrgAndSchoolData.action?parentId="+treeNode.id+"&hasEduOrgan="+settingNew.conf.hasEduOrgan+"&hasSchool="+settingNew.conf.hasSchool+"&rdm="+Math.random();
	 }
	 
	
 };

 /**
  * 获取路径信息
  * @param 树id
  */
 function getTreeNodesAttr(treeId, currentSelectedNode){
 	var treeNode = currentSelectedNode;
 	var treeNodes = [];
 	treeNodes.push(treeNode[currentTargetAttr]);
 	var parentNode = treeNode.getParentNode();
 	while(null != parentNode){
 		treeNodes.push(parentNode[currentTargetAttr]);
 		parentNode = parentNode.getParentNode();
 	}
 	return treeNodes;
 }

 /**
  * 刷新树
  * @param treeNodes 路劲数组
  * @param treeId 树id
  */
 function refreshTree(treeNode, treeId){
 	var zTree = $.fn.zTree.getZTreeObj(treeId);
     var currentNodes = zTree.getNodesByParam(currentTargetAttr, currentTreeNode[currentTreeNode.length-1], treeNode);
     if (currentNodes.length>0){
 	    zTree.selectNode(currentNodes[0]);//选择点  
 	    if(null == currentNodes[0].children){
 	 	    zTree.setting.callback.onClick(null, zTree.setting.treeId, currentNodes[0]);//调用事件
 	 	    currentTreeNode.pop();
 	 	    return;
 	    }
 	    else{
 	 	    zTree.settingNew.callback.onClick(null, zTree.setting.treeId, currentNodes[0]);//调用事件
 	 	    currentTreeNode.pop();
 	        refreshTree(currentNodes[0], treeId);
 	        return;
 	    }

     }
 }

 /**
  * 异步加载成功回调函数
  * @param event
  * @param treeId
  * @param treeNode
  * @param msg
  */
 function zTreeOnAsyncSuccess(event, treeId, treeNode, msg){
		refreshTree(treeNode, treeId);
		$(".ztree ul").each(function(){
			if($(this).css("overflow") == "hidden"){
				$(".ztree ul").css("overflow", "visible");
			}
		});
		if(settingNew.conf.isChange==1){
			modifyNodeNameByAttr("市辖区", treeId, "直属校", "name");
		}
	}

 /**
  * 设置字体颜色
  * @param treeId
  * @param treeNode
  * @returns
  */
function setFontCss(treeId, treeNode){
//	 if(treeNode.type==1){
//		return {color:"#ff0033"};
//	 }
//	 if(treeNode.type==2){
//		return {color:"#0033cc"};
//	 }
	 return (!!treeNode.highlight) ? {color:"#ff0000", "font-weight":"bold"} : {color:"#666", "font-weight":"normal"};
}

/**
 * 删除指定属性的节点 可删除多个
 * @param treeNodeId 逗号隔开的多个要删除的节点标识
 * @param treeId 树的id
 * @param currentAttr  用来查找要删除节点的属性
 */
function deleteNodeByAttr(treeNodeId, treeId, currentAttr){
	var zTree = $.fn.zTree.getZTreeObj(treeId);
	if(undefined == currentAttr || null == currentAttr || "" == currentAttr){
		currentAttr="id";
	}
	var nodesList = treeNodeId.split(",");
 	for(var i=0;i<nodesList.length;i++){
	    var nodes = zTree.getNodesByParam(currentAttr, nodesList[i], null);
	    zTree.removeNode(nodes[0]);
	}
}

/**
 * 根据指定属性修改树节点的展示名称
 * @param currentid 查找标识
 * @param treeId  树的id
 * @param nodeName 修改后的名称
 * @param currentAttr 用来查找要删除节点的属性
 */
function modifyNodeNameByAttr(currentid, treeId, newName, currentAttr){
	var zTree = $.fn.zTree.getZTreeObj(treeId);
	if(undefined == currentAttr || null == currentAttr || "" == currentAttr){
		currentAttr="id";
	}
	var nodes = zTree.getNodesByParam(currentAttr, currentid, null);
	if(nodes == null || nodes.length==0){
		return;
	}
	nodes[0].name=newName;
	zTree.updateNode(nodes[0]);
}

 /**
  * zTree初始化
  * @param domId // zTree占位符id
  * @param hasEduOrgan 是否加载教育机构节点
  * @param hasSchool 是否加载学校节点
  * @param TreeNodePath 要打开的路劲
  * @param treeNodeAttr 需要打开的属性名
  * @param treeType 树类型 1表示区域树 2表示学校树
  */
function zTreeInit(treeId, hasEduOrgan, hasSchool, treeNodePath, treeNodeAttr, treeType, ischange,hasDepartment){
	$.ajax({
		type:'post',
		//url:'manage/gisarea_loginUserGisAreaInfo.action?type='+treeType+'&hasEduOrgan='+hasEduOrgan+'&hasSchool='+hasSchool,
		url:'manage/gisarea_loginUserGisAreaInfo.action?type='+treeType,
		success: function(transport){
			//根据角色获取区域信息后添加
			if(transport.length == 0){
				if(treeType == 1){
//				    treeNode = treeRootNode;
				    var resultObj =$.ajax({url:"manage/gisarea_getSysOrgan.action?rdm="+Math.random(),async:false});
					var eduorg=eval('(' + resultObj.responseText + ')');
//					treeNode = [{"id":"2000000001000000013", "name":"安徽省教育厅","isParent":true ,type:'1'}];
					treeNode = [{"id":eduorg.id, "name":eduorg.fullName,"isParent":true ,type:'0'}];
					treeRootNode = treeNode;
				}
				else if(treeType == 2){
					treeNode = [{"id":"2134000016000000015", "name":"测试学校","isParent":true}];
					treeRootNode = treeNode;
				}
				// add by dongzhou2
				else if(treeType == 3){
					var resultObj =$.ajax({url:"manage/gisarea_getSysOrgan.action?rdm="+Math.random(),async:false});
					var eduorg=eval('(' + resultObj.responseText + ')');
//					treeNode = [{"id":"2000000001000000013", "name":"安徽省教育厅","isParent":true ,type:'1'}];
					treeNode = [{"id":eduorg.id, "name":eduorg.fullName,"isParent":true ,type:'1'}];
					treeRootNode = treeNode;
				}
			}
			else{
				var treeNode= transport;
				if(null != treeNode){
					treeRootNode.splice(0,1);
					treeRootNode = treeNode;
				}
			}
			if(null != hasEduOrgan || "" != hasEduOrgan){
				settingNew.conf.hasEduOrgan = hasEduOrgan;
			}
			if(null != hasSchool || "" != hasSchool){
				settingNew.conf.hasSchool = hasSchool;
			}
			// add by dongzhou2
			if(null != hasDepartment || "" != hasDepartment){
				settingNew.conf.hasDepartment=hasDepartment;
			}
			if(null != treeType || undefined == treeType){
				settingNew.conf.treeType = treeType;
			}
			if(ischange==1){
				settingNew.conf.isChange = ischange;
			}
			$.fn.zTree.init($("#"+treeId), settingNew, treeRootNode);
			openNodeByPath(treeId, treeNode, treeNodePath, treeNodeAttr);
		},
		error: function(){
			alert("获取用户信息失败！");
			return false;
		}
	});
 }

/**
 * zTree初始化
 * @param domId // zTree占位符id
 * @param treeNode //初始化根节点
 * @param hasEduOrgan 是否加载教育机构节点
 * @param hasSchool 是否加载学校节点
 * @param TreeNodePath 要打开的路劲
 * @param treeNodeAttr 需要打开的属性名
 * @param treeType 树类型 1表示区域树 2表示学校树
 * @param ischange 前台改变属树中节点展示
 */
function  zTreeInitByRootSchool(treeId, treeNode, hasEduOrgan, hasSchool, treeNodePath, treeNodeAttr, treeType, ischange,hasDepartment){
		if(null != treeNode){
			treeRootNode.splice(0,1);
			treeRootNode = treeNode;
		}
		if(null != hasEduOrgan || "" != hasEduOrgan){
			settingNew.conf.hasEduOrgan = hasEduOrgan;
		}
		if(null != hasSchool || "" != hasSchool){
			settingNew.conf.hasSchool = hasSchool;
		}
		if(null != treeType){
			settingNew.conf.treeType = treeType;
		}
		if(ischange==1){
			settingNew.conf.isChange = ischange;
		}
		if(null!=hasDepartment || "" != hasDepartment){
			settingNew.conf.hasDepartment=hasDepartment;
		}
		$.fn.zTree.init($("#"+treeId), settingNew, treeRootNode);
		openNodeByPath(treeId, treeNode, treeNodePath, treeNodeAttr);
}

/**
 * 打开树中指定节点
 * @param treeId 树对应的id
 * @param treeNode 初始化节点信息
 * @param treeNodePath 加载打开路劲
 * @param treeNodeAttr 节点属性
 */
function openNodeByPath(treeId, treeNode, treeNodePath, treeNodeAttr){
	var zTree = $.fn.zTree.getZTreeObj(treeId);
	zTree.expandAll(false);
	if(undefined != treeNodePath && null != treeNodePath && "" != treeNodePath){
		currentTreeNode = treeNodePath.split(',');
	}
	if(undefined == treeNodeAttr || null == treeNodeAttr || "" == treeNodeAttr){
		treeNodeAttr="id";
	}
	currentTargetAttr = treeNodeAttr;
	if("" == currentTreeNode){
		  currentTreeNode.push(treeNode[0][currentTargetAttr]);
	  }
    refreshTree(null, treeId);
}

/**
 * 搜索该名称的树节点
 * @param treeId
 * @param nodeName
 */
function seachNode(treeId,nodeName){
	if(nodeName==""){
		removeClass(treeId);
		return;
	}
	removeClass(treeId);
	var zTree = $.fn.zTree.getZTreeObj(treeId);
	nodeList = zTree.getNodesByParamFuzzy("name", nodeName);
	/**
	for (var i=0; i<nodeList.length; i++) {
	   zTree.settingNew.callback.onClick(null, treeId, nodeList[i]);
	   zTree.setting.callback.onClick(null, zTree.setting.treeId, nodeList[i]);
	   zTree.expandNode(nodeList[i], true, true, false, true);
	}
    $.each(nodeList, function (index, node) {
    	zTree.selectNode(node, true);
    });
    */
	updateNodes(true,treeId);
}

/**
 * 将搜索到的节点进行高亮
 * @param highlight
 * @param treeId
 */
function updateNodes(highlight,treeId) {
	var zTree = $.fn.zTree.getZTreeObj(treeId);
	for( var i=0, l=nodeList.length; i<l; i++) {
		nodeList[i].highlight = highlight;
		zTree.updateNode(nodeList[i]);
	}
}
//function getFontCss(treeId, treeNode) {
//	return (!!treeNode.highlight) ? {color:"#A60000", "font-weight":"bold"} : {color:"#333", "font-weight":"normal"};
//}

/**
 * 移除搜索节点的样式
 */
function removeClass(treeId){
	var zTree = $.fn.zTree.getZTreeObj(treeId);
	for( var i=0, l=nodeList.length; i<l; i++) {
		nodeList[i].highlight = false;
		zTree.updateNode(nodeList[i]);
	}
}



